package com.pactera.asmp.server.restfullapi;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.pactera.asmp.server.common.constants.ResultCode;
import com.pactera.asmp.server.common.logprint.utils.CollectionUtils;
import com.pactera.asmp.server.entity.ProjectMastercBindDto;
import com.pactera.asmp.server.entity.ProjectMastercDbcBindDto;
import com.pactera.asmp.server.entity.ResponseResult;
import com.pactera.asmp.server.pojo.MastercProjectDbcVo;
import com.pactera.asmp.server.pojo.TProjectMasterComputerDbc;
import com.pactera.asmp.server.service.IProjectMastercDbcService;

import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("/api/v1")
@Slf4j
public class RestfulProjectMastercDbcApi {

    @Autowired
    private IProjectMastercDbcService projectMastercDbcService;

    /**
     * 上位机批量绑定 dbc 文件
     * 
     * @param projectMastercDbcBindDto
     * @return
     */
    @PostMapping("/proMasterc/batchBindDbc")
    public ResponseResult batchBindDbc(@RequestBody ProjectMastercDbcBindDto projectMastercDbcBindDto) {
        log.debug("/api/v1/proMasterc/batchBindDbc projectMastercDbcBindDto: {}", projectMastercDbcBindDto);
        ResponseResult responseResult;
        if (null == projectMastercDbcBindDto || CollectionUtils.isEmpty(projectMastercDbcBindDto.getList())) {
            responseResult = new ResponseResult(ResultCode.PARAM_IS_BLANK);
            return responseResult;
        }
        responseResult = new ResponseResult(ResultCode.SUCCESS);
        responseResult.setData(projectMastercDbcService.updateDbc(projectMastercDbcBindDto));
        return responseResult;
    }

    /**
     * 项目上位机批量解绑
     * 
     * @param projectMastercDbcBindDto
     * @return
     */
    @PostMapping("/proMasterc/batchUnBind")
    public ResponseResult batchUnBind(@RequestBody List<TProjectMasterComputerDbc> projectMastercDbcs) {
        log.debug("/api/v1/proMasterc/batchUnBind projectDeviceDbcs: {}", projectMastercDbcs);
        ResponseResult responseResult;
        if (null == projectMastercDbcs ) {
            responseResult = new ResponseResult(ResultCode.PARAM_IS_BLANK);
            return responseResult;
        }
        responseResult = new ResponseResult(ResultCode.SUCCESS);
        responseResult.setData(projectMastercDbcService.batchDelete(projectMastercDbcs));
        return responseResult;
    }

    /**
     * 根据 projectId 或 masterComputerId 获取项目上位机绑定关系
     * 
     * @param projectId
     * @param deviceId
     * @return
     */
    @GetMapping("/proMasterc/list")
    public ResponseResult projectDeviceList(Integer projectId, Integer masterComputerId) {
        log.debug("/api/v1/proMasterc/list projectId: {}, masterComputerId: {}", projectId, masterComputerId);
        ResponseResult responseResult = new ResponseResult(ResultCode.SUCCESS);
        responseResult.setData(projectMastercDbcService.selective(projectId, masterComputerId));
        return responseResult;
    }

    /**
     * 批量绑定项目上位机
     * 
     * @param projectMastercBindDto
     * @return
     */
    @PostMapping("/proMasterc/batchBind")
    public ResponseResult batchBind(@RequestBody ProjectMastercBindDto projectMastercBindDto) {
        log.debug("/api/v1/proMasterc/batchBind projectMastercBindDto: {}", projectMastercBindDto);
        ResponseResult responseResult;
        if (null == projectMastercBindDto || null == projectMastercBindDto.getProjectId()) {
            responseResult = new ResponseResult(ResultCode.PARAM_IS_BLANK);
            return responseResult;
        }
        responseResult = new ResponseResult(ResultCode.SUCCESS);
        responseResult.setData(projectMastercDbcService.batchBind(projectMastercBindDto));
        return responseResult;
    }

    /**
     * 修改上位机信息
     * 
     * @param mastercProjectDbcVo
     * @return
     */
    @PostMapping("/proMasterc/mastercBind")
    public ResponseResult mastercBind(@RequestBody MastercProjectDbcVo mastercProjectDbcVo) {
        log.info("/api/v1/proDevice/mastercBind mastercProjectDbcVo: {}", mastercProjectDbcVo);
        return projectMastercDbcService.mastercBind(mastercProjectDbcVo);
    }

    /**
     * 根据上位机ID获取所有项目绑定的DBC文件
     * 
     * @param masterComputerId
     * @return
     */
    @GetMapping("/proMasterc/dbcsByMastercId")
    public ResponseResult getProjectMastercDbcsByMastercId(@RequestParam("masterComputerId") Integer masterComputerId) {
        log.debug("/api/v1/proMasterc/dbcsByMastercId masterComputerId: {}", masterComputerId);
        return ResponseResult.defaultSuccess(projectMastercDbcService.getProjectMastercDbcsByMastercId(masterComputerId));
    }
}
